www.gusucode.com > C++ 游戏类库源码程序 > C++ 游戏类库源码程序\code\游戏类库\编辑.cpp

    /*
[学VC、编游戏]
编著、程序设计:唐明理 2004.7
E-mail:  cqtml@163.com
*/
#include "stdafx.h"
#include "编辑.h"

gmedit:: gmedit(){}
gmedit::~gmedit(){}	


void gmedit::initedit()
{	pen. CreatePen(PS_SOLID,1,RGB(0x80,0x80,0x80));
	penz.CreatePen(PS_SOLID,1,RGB(0x80,0x0,0x0));
	initmap();
}
void gmedit::exitedit()
{	DeleteObject(pen);
	DeleteObject(penz);
	exitmap();
}

void gmedit::Insert(int no,int dorj) 
{	if(rs>SU_LIANG-2)
		{AfxMessageBox("对象数超出!");	 return;}
	 int i=rs;
	 man[i].jisu=i+1;
	 if(dorj==0) man[i].lb=2;
	 else		 man[i].lb=1;
	 man[i].fw=1;
	 man[i].zs=0;
	 man[i].p=no;
	 man[i].js=dorj*(no-tml);
	 if(man[i].lb!=2) setman(i);
	 getobjwh(i);
	 man[i].xix=scrx+WIDTH/2;
	 man[i].xiy=scry+HEIGHT/2;	
	 man[i].x0=man[i].xix;
	 man[i].y0=man[i].xiy;
	 if(dorj==1)
	 {	int x=man[i].x0+(100-rand()%200);
		int y=man[i].y0+(100-rand()%200);
		FindPath(i,x,y);
	 }
	 rs++;
}

void gmedit::Delete()
{	if(gno==0||rs<2)	return;
	for(int i=0;i<rs;i++)
	  if(man[i].jisu==gno)
		{for (int j=i;j<rs-1;j++)	man[j]=man[j+1];
		 rs--;
		 for(int k=0;k<rs;k++)
			if(man[k].jisu!=0) man[k].jisu=k+1;	
		 return;
		}
}

void gmedit::getno(int x,int y)
{	gno=-1;
	for(int i=0;i<rs;i++)
		{if((x+scrx)>(man[i].xix-man[i].w/2+2)&&(x+scrx)<(man[i].xix+man[i].w/2+2)&&
			(y+scry)>(man[i].xiy-man[i].h+10)&&(y+scry)<(man[i].xiy+10)) 
			{gno=man[i].jisu;
			 x1=man[i].xix;	
			 y1=man[i].xiy;	
			 return;
			}
		}
}

void gmedit::getobj(HDC hdc,int i)
{	int x,y=HEIGHT+19,w=48,h=47;
	CDC* bc = CDC::FromHandle (hdc);
	CPen *old;
	old=bc->SelectObject(&pen0);
	for(int j=0;j<12;j++)
	{x=j*50+2;
	 bc->MoveTo(x,y);
	 bc->LineTo(x+w,y);	 bc->LineTo(x+w,y+h);
	 bc->LineTo(x,y+h);	 bc->LineTo(x,y);
	}
	bc->SelectObject(old);
	if(i<0) return;
	old=bc->SelectObject(&pen1);
	x=(i-tml)*50+2;
	bc->MoveTo(x,y);
	bc->LineTo(x+w,y);	 bc->LineTo(x+w,y+h);
	bc->LineTo(x,y+h);	 bc->LineTo(x,y);
	bc->SelectObject(old);
	CDC::DeleteTempMap( );
	BitBlt(hdc,WIDTH+10-1, SCRHE-100-1,w+1,h+1,hdc,x,y,SRCCOPY);
}

void gmedit::getwp(HDC hdc,int dorj)
{	CString cc;
	int xx=2;	int yy=HEIGHT+18;
	int wh=50;
	if(edi==0)
	{CBrush Brush(RGB(0xd0,0xd0,0xd0));
	 SelectObject(hdc,Brush);
	 PatBlt(hdc,xx,yy,WIDTH,wh,PATCOPY);
	 PatBlt(hdc,WIDTH+8, SCRHE-102,50,50,PATCOPY);
	 return;
	}
	else 
		BitBlt(hdc,xx,yy,WIDTH,wh,BkDC0,0,0,SRCCOPY);
	int p=0;
	int tml0;
	if(dorj==1) tml0=0;
	else		tml0=tml;
	for(int i=tml0;i<tml0+12;i++)
	{int p0;
	 if(dorj==0){cc="景";p0=i;}
	 else		{cc="兽";p0=i*400+1*5;}
	 if(getpic(cc,p0)==FALSE) 
				{getobj(hdc,-1);return;}
	 float wi0=(float)w,he0=(float)h;
	 float bl=wi0/he0;
	 if(wi0<wh&&he0<wh) goto aa;
	 if(bl>1)	{wi0=(float)wh;he0=wi0/bl;}
	 else		{he0=(float)wh;wi0=he0*bl;}
aa:	 int x0=(int)(wh-wi0)/2+p*wh+xx;
	 int y0=(int)(wh-he0)/2+yy;
	 TransparentBlt2(hdc,x0,y0,(int)wi0,(int)he0,MemDC,0,0,w,h,RGB(255,255,255));
	 p++;
	}
	getobj(hdc,-1);
}

void gmedit::showline()
{	 CDC* bc = CDC::FromHandle (BkDC1);
	 CPen *old;
	 old=bc->SelectObject(&pen);
	 for(int i=GY;i<HEIGHT;i=i+GY)
		 {bc->MoveTo(0,i);  bc->LineTo(WIDTH,i); }
	 for(int j=GX;j<WIDTH;j=j+GX)
		 {bc->MoveTo(j,0);  bc->LineTo(j,HEIGHT);}
	 bc->SelectObject(old);

	 old=bc->SelectObject(&penz);
	 for(i=0;i<WIDTH+GX;i=i+GX)
	   for(j=0;j<HEIGHT+GY;j=j+GY)
		 {int x=(i+scrx)/GX-1,y=(j+scry)/GY-1;
		  if(m_fid.map[x][y]=='1')
		  {	bc->MoveTo(i,j);	bc->LineTo(i-GX,j-GY);
			bc->MoveTo(i-GX,j);	bc->LineTo(i,j-GY);
			bc->MoveTo(i,j);
			bc->LineTo(i,j-GY);	bc->LineTo(i-GX,j-GY);
			bc->LineTo(i-GX,j);	bc->LineTo(i,j);
		  }
		 }

	for(i=0;i<rs;i++)
	 if(gno==man[i].jisu)
		{int w=man[i].w;
		 int h=man[i].h;
		 int x=man[i].xix-scrx-w/2;	
		 int y=man[i].xiy-scry-h;
		 old=bc->SelectObject(&pen1);
		 bc->MoveTo(x,y);
		 bc->LineTo(x+w,y);	 bc->LineTo(x+w,y+h);
		 bc->LineTo(x,y+h);	 bc->LineTo(x,y);
		 bc->SelectObject(old);
		}
	 bc->SelectObject(old);
	 CDC::DeleteTempMap( );
}

void gmedit::savemap(CString name)
{ FILE *f;
  int i,j;
  f=fopen(name,"w");
  if(f==NULL) {AfxMessageBox("不能写文件!!!");return;}
  fprintf(f,"%s\n",mapbak);	
  fprintf(f,"%d\n",rs);	
  for (i=0;i<rs;i++)
	{fprintf(f,"%d,",man[i].jisu);
	 fprintf(f,"%d,",man[i].lb);
	 fprintf(f,"%d,",man[i].js);

	 fprintf(f,"%d,",man[i].p);	
	 fprintf(f,"%d,",man[i].xix);
	 fprintf(f,"%d,\n",man[i].xiy);	
	}
  fclose(f);
  char cc[256];
  strcpy(cc,name);cc[lstrlen(name)-3]=0;
  strcat(cc,"map");
  f=fopen(cc,"w");
  if(f==NULL) {AfxMessageBox("不能写文件!!!");return;}
  fprintf(f,"%d,%d\n",WIDTH*SCRP0/GX,HEIGHT*SCRP0/GY);
  for(i=0;i<WIDTH*SCRP0/GX;i++)
  {for(j=0;j<HEIGHT*SCRP0/GY;j++)
	 fprintf(f,"%c",m_fid.map[i][j]);
   fprintf(f,"\n");
  }
  fclose(f);
}

void gmedit::newmap()
{	rs=1;
	man[0].jisu=0;
	man[0].lb=0;
	man[0].p=0;	
	man[0].xix=WIDTH/2;	
	man[0].xiy=HEIGHT/2;

	man[0].x0=man[0].xix;
	man[0].y0=man[0].xiy;
	man[0].fw=1;
	man[0].zs=0;
	man[0].pk =-1;
	man[0].fid=-1;
	man[0].zd=0;
	setman(man[0].jisu);
	getobjwh(0);
	m_fid.map_w=WIDTH*SCRP0/GX;	//
	m_fid.map_h=HEIGHT*SCRP0/GY;//
	int i,j;
	for(i=0;i<m_fid.map_w;i++)
		for(j=0;j<m_fid.map_h;j++)
				m_fid.map[i][j]='0';	
	getsmap();
}